<?php

namespace app\common\service;

class GaoDeService extends ApiService
{

    private $url = 'https://restapi.amap.com/v5/ip';
    private $key;

    public function __construct()
    {
        $this->key = env('gaode.key', '');
    }

    public function getIpInfo($ip = '')
    {
        if (!$ip) {
            $ip = '125.42.98.142';
        }
        $param = [
            'ip' => $ip,
            'key' => $this->key
        ];
        $url = $this->url . '?' . http_build_query($param);
        $res = HttpService::getRequest($url);
        $res = json_decode($res, true);
        if (!isset($res['status'])) {
            $this->setError('请求高德地图获取ip地址信息失败');
            return false;
        }
        if ($res['status'] != 1) {
            $this->setError($res['info']);
            return false;
        }
        return $res;

    }

    public function getLoginLocation($ip)
    {
        if (!checkIp($ip)) {
            return 'xx xx';
        }
        if (in_array($ip, ['127.0.0.1'])) {
            return '本地';
        }
        $res = $this->getIpInfo($ip);
        if (!$res) {
            return '';
        }
        return $res['country'] . '|' . $res['province'] . '|' . $res['city'] . '|' . $res['isp'];

    }
}